<?php
if (!defined('ROOT')) {
   die("Impossible de voir ce fichier directement.");
}
/**
 *
 *EXEMPLE UTILISATION
 * $inputs = array(
      "id"          => array("name" => "id","type" => "hidden"),
      "lastname"    => array("name" => "lastname", "label" => "Nom :"),
      "firstname"   => array("name" => "firstname", "label" => "Prénom :"),
      "email"       => array("name" => "email", "label" => "E-mail :"),
      "password"   => array("name" => "password", "label" => "Mot de passe :", "type" => "password"),
           );
 
   
  echo $user->getForm($inputs);
*/
class Form
{
    var $table;
    var $model;
    var $inputs;
    
    var $action;
    var $method;
    var $label;
    var $id;
    
    var $form;
    
    function __construct($model,$inputs, $method='post', $action="", $id='', $submitlabel= 'Valider')
    {
      $this->model = $model;
      $this->inputs = $inputs;
      $this->action = $action;
      $this->method = $method;
      $this->label = $submitlabel;
      $this->id   = $id;
    }
   
   function getForm()
   {
     
      $this->form = "";
      //Ajout des datas supplémentaires si echec de la validation apres envoi du POST
      if (!empty($_SESSION['PostData'])) $_POST = $_SESSION['PostData'];
      if (!empty($_SESSION['PostDataMessage'])) $this->form .= "<p style='color:red;padding-left: 160px;padding-bottom: 10px;'>".$_SESSION['PostDataMessage']."</p>";

      $this->form .= "<form id='".$this->model->table."' method='".$this->method."' id='".$this->id."' action='".$this->action."' >";
      
      $this->form .= "<fielset>";      
       foreach(array_keys($this->inputs) as $i)
       {
         $this->form .= "<p>".$this->getFormField($this->inputs[$i])."</p>";
       }
     
      $this->form .= '<div class="spacer_20px"></div>';
      //Submit button
      $this->form .= '<div class="button button-orange"><span class=" form_button clearfix">';
      $this->form .= '<input type="submit" class="submit" name="submit" value="'.$this->label.'" />';
      $this->form .= '</span></div>';
      
      $this->form .= "</fielset>";
      $this->form .= "</form>";
      
      //Delete datas
      unset($_SESSION['PostData']);
      unset($_SESSION['PostDataMessage']);

      return $this->form;
      
   }
   /**
    * $values = "type" => Type de l'élement HTML a afficher (text, hidden, select..)
    *  "name" =>Nom de l'élément, "value" => Valeur par défaut, "class" => class css
    * "input" => array pour l'élément Select
    */
   function getFormField($values)
   {
      //Valeurs par def
      if (!isset($values['type']))     $values['type'] = 'text';
      if (!isset($values['label']))    $values['label'] = $values['name'];
      if (!isset($values['class']))    $values['class'] = 'required';
      if (!isset($values['input']))    $values['input'] = array();
      if (!isset($values["value"]))    $values["value"] = "";
      if (isset($_POST[$values['name']])) $values["value"] = $_POST[$values['name']];
      
      if (isset($this->model->fields[$values['name']]))
         $values["value"] = $this->model->fields[$values['name']];
      
      if ($values['type'] == 'hidden' && $values['class'] == 'required')
         $values['class'] = '';
      
      $html = "<label for='".$values['name']."'>".$values['label']."</label>";
      switch($values['type'])
      {
         case "text":
            $html .= "<input type='text' id='".$values["name"]."' name='".$values["name"]."' class='".$values["class"]."' value='".$values["value"]."' />";
            break;
         case "submit":
            $html .= "<input type='submit' id='".$values["name"]."' name='".$values["name"]."' class='".$values["class"]."' value='".$values["value"]."' />";
            break;
         case "hidden":
            $html = "";
            $html .= "<input type='hidden' id='".$values["name"]."' name='".$values["name"]."' class='".$values["class"]."' value='".$values["value"]."' />";
            break;
         case "textarea":
            $html .= "<textarea type='text' id='".$values["name"]."' name='".$values["name"]."'>".$values["value"]."</textarea>";
            break;
         case "select":
            $html .= "<select id='".$values["name"]."' name=".$values["name"].">";
            foreach($values['input'] as $key => $val)
            {
               if ($values["value"] != "" && $values["value"] == $key)
               {
                  $html .= "<option value='$key' selected>$val</option>";
               }
               else
               {
                  $html .= "<option value='$key'>$val</option>";
               }
            }
            $html .= "</select>";
            break;
         case "password":
            $html .= "<input id='".$values["name"]."' type='password' name='".$values["name"]."' class='".$values["class"]."' value='".$values["value"]."' />";
            break;
         default:
            return "";
         
      }
      return $html;
    
   }
   
    
  





      
   

}

